using System;
using System.Collections.Generic;
using System.Text;
using System.Threading.Tasks;
using OfficeOpenXml.FormulaParsing.Excel.Functions.DateTime;
using Publicuse.Application;
using Publicuse.Autofac;
using Publicuse.Entity;
using Publicuse.Entity.AttributeUtil;
using Publicuse.Export;
using Publicuse.Interface;
using Publicuse.Util;
using SqlSugar;

namespace Publicuse.Appication
{
  /// <summary>
  /// 节假日信息管理
  /// </summary>
  public class HolidayApplication
  {
    private static BaesApplication baseApplication;
    private static HolidayApplication userApplication;

    public static HolidayApplication GetApplication(ServiceContainer container)
    {
      baseApplication = new BaesApplication(container);
      return userApplication;
    }

    static HolidayApplication()
    {
      userApplication = new HolidayApplication();
    }

    /// <summary>
    /// 获取假日详细信息
    /// </summary>
    /// <param name="Id"></param>
    /// <returns></returns>
    public async Task<CommonResult> GetDetail(int Id)
    {
      return await Task.Factory.StartNew(() =>
      {
        CommonResult result = new CommonResult();
        var list = baseApplication.mySqlSugar.QueryByExpression<Holiday>(
          o => o.Id == Id,
          out var msg
        );
        if (string.IsNullOrEmpty(msg) && list != null && list.Count > 0)
        {
          var spconf = list.FirstOrDefault();
          result.data = spconf;
        }
        result.msg = msg;
        return result;
      });
    }

    /// <summary>
    /// 获取节假日信息列表
    /// </summary>
    /// <param name="query"></param>
    /// <returns></returns>
    public async Task<CommonResult> GetList(Holiday query)
    {
      return await Task.Factory.StartNew(() =>
      {
        CommonResult result = new CommonResult();
        var tableName = TableAtributeUtil.GetTable<Holiday>();
        string sql = query.GetQueryString<Holiday>(null);
        HolidayQuery holidayQuery = new HolidayQuery()
        {
          tableName = tableName,
          sqlWhere = sql,
          sortFile = query.sortFile,
          sortType = query.sortType,
          files = query.files,
          pageIndex = query.pageIndex,
          pageSize = query.pageSize,
        };
        result = baseApplication
         .GetList<Holiday>(holidayQuery, false)
         .GetAwaiter()
         .GetResult();
        return result;
      });
    }

    /// <summary>
    /// 检查节假日名称是否存在
    /// </summary>
    /// <param name="hName"></param>
    /// <param name="hYear"></param>
    /// <param name="loginUserCode"></param>
    /// <param name="Id"></param>
    /// <returns></returns>
    public async Task<CommonResult> CheckHolidayName(
      string hName,
      string hYear,
      string loginUserCode,
      string Id = ""
    )
    {
      return await baseApplication.CheckDataExists<Holiday>(
        BusinessType.holiday,
        BusinessLogType.writeLogToDataBase,
        o => o.hName == hName && o.hYear == hYear.GetIntValue() && o.status != 9,
        "检查节假日名称是否存在",
        Id
      );
    }

    /// <summary>
    /// 根据年份获取节假日信息
    /// </summary>
    /// <param name="year"></param>
    /// <returns></returns>
    public async Task<CommonResult> GetListByYear(int year)
    {
      return await Task.Factory.StartNew(() =>
      {
        CommonResult result = new CommonResult();
        var table = TableAtributeUtil.GetTable<Holiday>();
        string sql = $"select Id,hName from {table} where hYear=@hYear and status<>-9;";
        var dt = baseApplication.mySqlSugar.GetDataTable(sql, new { hYear = year }, out var msg);
        if (dt != null && dt.Rows.Count > 0)
        {
          result.data = dt;
        }
        result.msg = msg;
        return result;
      });
    }

    /// <summary>
    /// 新增修改节假日信息
    /// </summary>
    /// <param name="holiday"></param>
    /// <returns></returns>
    public async Task<CommonResult> AddOrUpdate(Holiday info)
    {
      return await baseApplication.AddOrUpdate<Holiday>(
        info,
        BusinessType.holiday,
        BusinessLogType.writeLogToDataBase
      );
    }

    /// <summary>
    /// 删除节假日信息
    /// </summary>
    /// <param name="Id"></param>
    /// <param name="loginUserCode"></param>
    /// <param name="type"></param>
    /// <returns></returns>
    public async Task<CommonResult> Delete(string Id, string loginUserCode, string type)
    {
      return await baseApplication.Delete<Holiday>(
        Id,
        loginUserCode,
        type,
        BusinessType.holiday,
        (a, b, info) =>
        {
          return info.hName;
        }
      );
    }

    /// <summary>
    /// 还原节假日信息
    /// </summary>
    /// <param name="Id"></param>
    /// <param name="loginUserCode"></param>
    /// <returns></returns>
    public async Task<CommonResult> Reduction(string Id, string loginUserCode)
    {
      return await baseApplication.Reduction<Holiday>(
        Id,
        loginUserCode,
        BusinessType.holiday,
        (a, b, info) =>
        {
          return info.hName;
        }
      );
    }
  }
}
